//
//  PromotionView.m
//  JiaoX
//
//  Created by yinqy on 15/1/12.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "PromotionView.h"
#import "TimeUtils.h"
#import "Present.h"
#import "ProdDetailsUitls.h"
#import "ProdPromotionShowView.h"

#define LABEL_FONT_SIZE 13
#define TABLE_CELL_HEIGHT 44

@interface PromotionView()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    
    UILabel *_priceLabel;
    UILabel *_countLabel;
    UILabel *_timeLabel;
    NSTimer *_countDownTimer;
    BOOL haveTime;
}

@end
@implementation PromotionView

-(void)setPromotion:(ProdPromotion *)promotion{
    if (promotion==nil) {
        [self setBounds:CGRectMake(0, 0, 0, 0)];
        return;
    }
    [self setUserInteractionEnabled:YES];
    self.width = JXScreenW-20;
    
    
    CGFloat maginTop = 0;
    _promotion = promotion;
    

    if(![Utils isBlankString:promotion.price]){
        haveTime = YES;
        
        UIImageView *imgview = [[UIImageView alloc] initWithImage:[UIImage resizedImage:@"bg_time_limit"]];
        [imgview setWidth:self.width];
        [imgview setHeight:imgview.image.size.height];
        UIView *proView = [[UIView alloc]init];
        [proView setSize:imgview.size];
        [proView addSubview:imgview];
        
        //添加限时抢购价
        NSString *priceStr = [NSString stringWithFormat:@"[限时抢]￥%@",promotion.price];
        CGSize priceSize = [priceStr sizeWithFont:[UIFont systemFontOfSize:LABEL_FONT_SIZE]];
        _priceLabel = [[UILabel alloc]init];
        [_priceLabel setTextColor:JXColor(255, 102, 102)];
        [_priceLabel setText:priceStr];
//        [_priceLabel setAttributedText:[self getAttribtStrin:priceStr]];
        [_priceLabel setFont:[UIFont systemFontOfSize:LABEL_FONT_SIZE]];
        [_priceLabel setFrame:CGRectMake(JXPublicInset, 0, priceSize.width, proView.height)];
        [proView addSubview:_priceLabel];
        
        //添加限购数量
        NSString *countStr = [NSString stringWithFormat:@"限%d件",promotion.limitCount];
        CGSize countSize = [countStr sizeWithFont:[UIFont systemFontOfSize:LABEL_FONT_SIZE]];
        _countLabel = [UILabel new];
        [_countLabel setTextColor:JXColor(255, 102, 102)];
        [_countLabel setText:countStr];
        [_countLabel setFont:[UIFont systemFontOfSize:LABEL_FONT_SIZE]];
        [_countLabel setFrame:CGRectMake(CGRectGetMaxX(_priceLabel.frame)+JXPublicInset*0.5, 0, countSize.width, proView.height)];
        [proView addSubview:_countLabel];
        
        //添加剩余时间

        _timeLabel = [UILabel new];
        [_timeLabel setTextColor:JXColor(255, 102, 102)];
        [_timeLabel setFont:[UIFont systemFontOfSize:LABEL_FONT_SIZE]];
        [_timeLabel setTextAlignment:NSTextAlignmentRight];
        [_timeLabel setFrame:CGRectMake((JXScreenW-20)/2, 0, (JXScreenW-20)/2-JXPublicInset, proView.height)];
        NSString *timeStr = [NSString stringWithFormat:@"仅剩 %@",[TimeUtils getFormatRemainTime:[TimeUtils getRemainTime:self.promotion.endTime]]];
        [_timeLabel setText:timeStr];
        [proView addSubview:_timeLabel];
        _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeLabel) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:_countDownTimer forMode:NSDefaultRunLoopMode];
        
        maginTop = CGRectGetMaxY(_timeLabel.frame)+ 5;
        [self addSubview:proView];
    }
    
    if(promotion.presentList!=nil || promotion.presentList.count!=0){
        _tableView = [[UITableView alloc]init];
        _tableView.frame = CGRectMake(0, maginTop, JXScreenW, TABLE_CELL_HEIGHT*promotion.presentList.count);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        maginTop = CGRectGetMaxY(_tableView.frame);
        [self addSubview:_tableView];
    }
    
    [self setBounds:CGRectMake(0, 0, JXScreenW-20, maginTop)];
}


#pragma Mark - tableView delegte&datasource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _promotion.presentList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"PROMOTIONCELL";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell==nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        
        UIButton *button = [[UIButton alloc]init];
        [button setUserInteractionEnabled:NO];
        [button setBackgroundImage:[UIImage resizedImage:@"bg_gift"] forState:UIControlStateNormal];
        [button setHeight:button.currentBackgroundImage.size.height];
        [button setWidth:JXScreenW-20];
        [button.titleLabel setFont:SYS_FONT(13)];
//        [button setX:10];
        [button setTitleColor:JXColor(255, 102, 102) forState:UIControlStateNormal];
        [button setTag:101];
//        UIButton *btn = [ProdDetailsUitls getCellBtnWithImageName:@"product_gift" font:[UIFont systemFontOfSize:13] color:JXColor(102, 102, 102) cellHeight:cell.height];
//        [btn setTag:101];
        [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [cell addSubview:button];
    }
    Present *present = _promotion.presentList[indexPath.row];
    UIButton *btn = (UIButton *)[cell viewWithTag:101];
    [btn setTitle:[NSString stringWithFormat:@"[买就赠]%@",present.name] forState:UIControlStateNormal];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Present *present = _promotion.presentList[indexPath.row];
    JXLog(@"点击了第%ld个赠品 ",(long)indexPath.row);
    ProdPromotionShowView *view = [[ProdPromotionShowView alloc]initWithImageUrl:present.img title:present.name];
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

-(void)timeLabel{
    long long reMainTime = [TimeUtils getRemainTime:self.promotion.endTime];
    if(reMainTime<=0){
        [_countDownTimer invalidate];
        //在这写倒计时完了之后需要做的事情
    }else{
        NSString *timeStr = [NSString stringWithFormat:@"仅剩 %@",[TimeUtils getFormatRemainTime:reMainTime]];
        [_timeLabel setText:timeStr];
    }
}

-(NSAttributedString *)getAttribtStrin:(NSString *)string{
    NSAttributedString *str = [[NSAttributedString alloc]initWithString:string];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithAttributedString:str];
    [text addAttribute:NSForegroundColorAttributeName value:JXColor(77, 77, 77)  range:NSMakeRange(0, 6)];
    [text addAttribute:NSForegroundColorAttributeName value:JXThemeColor  range:NSMakeRange(6, string.length-6)];
    return text;
}

-(void)invalidateTimer{
    [_countDownTimer invalidate];
}

-(void)layoutSubviews{
    if(!_tableView)return;
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0,JXPublicInset,0,0)];
    }
    
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsMake(0,JXPublicInset,0,0)];
    }
}

@end
